﻿Imports EHaskins.Frc.Dashboard
Imports System.IO

Public Class FrcDashUserDataProcessor
    Inherits DefaultUserDataProcessor
    Implements IFrcDashUserDataProcessor

    Public Sub New()
        SetId(1103, 2)
    End Sub

    Public Event InvalidateCurrentProcessor As EventHandler Implements IFrcDashUserDataProcessor.InvalidateCurrentProcessor
    Public Event NewUserDataReceived As EventHandler Implements IFrcDashUserDataProcessor.NewUserDataReceived
    Protected Sub RaiseNewUserDataReceived()
        RaiseEvent NewUserDataReceived(Me, New EventArgs())
    End Sub
    Protected Sub RaiseInvalidateCurrentProcessor()
        RaiseEvent InvalidateCurrentProcessor(Me, New EventArgs())
    End Sub

    Dim _processorOriginID As Integer
    Dim _processorID As Integer

    Dim _userBytes As Byte()

    Public Overrides Function Parse(ByVal data() As Byte) As Byte()
        Dim myData = MyBase.Parse(data)
        Return MyClass.ShallowParse(myData)
    End Function

    Public Overrides Function ShallowParse(ByVal data As Byte()) As Byte()
        If data.Length > 0 Then
            Dim reader As New BinaryReader(New MemoryStream(data))
            Dim startPos = reader.BaseStream.Position
            Try
                Dim bytesRead As Long
                Try
                    CurrentProcessorOriginID = reader.EReadUInt16()
                    CurrentProcessorID = reader.ReadByte()
                    bytesRead = reader.BaseStream.Position - startPos
                Catch ex As Exception
                    CurrentProcessorID = 0
                    CurrentProcessorOriginID = 0
                    bytesRead = 0
                    reader.BaseStream.Position = startPos
                End Try

                Dim userData = reader.ReadBytes(data.Length - bytesRead)
                Me.UserData = userData
                Return userData
            Catch ex As Exception
                Throw
            End Try
        End If
        Return Nothing
    End Function
    Public Property CurrentProcessorID() As Integer Implements IFrcDashUserDataProcessor.CurrentProcessorID
        Get
            Return _processorID
        End Get
        Set(ByVal value As Integer)
            If _processorID = value Then
                Return
            End If
            _processorID = value
            RaiseInvalidateCurrentProcessor()
            RaisePropertyChanged("CurrentProcessorID")
        End Set
    End Property
    Public Property CurrentProcessorOriginID() As Integer Implements IFrcDashUserDataProcessor.CurrentProcessorOriginID
        Get
            Return _processorOriginID
        End Get
        Set(ByVal value As Integer)
            If _processorOriginID = value Then
                Return
            End If
            _processorOriginID = value
            RaiseInvalidateCurrentProcessor()
            RaisePropertyChanged("CurrentProcessorOriginID")
        End Set
    End Property
    Public Overrides Property UserData() As Byte() Implements IFrcDashUserDataProcessor.UserData
        Get
            Return _userBytes
        End Get
        Set(ByVal value As Byte())
            _userBytes = value
            RaiseNewUserDataReceived()
            RaisePropertyChanged("UserData")
        End Set
    End Property
End Class
